﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using System.Collections.Generic;


using PoolGame2.Assets;
using PoolGame2.Managers;
using PoolGame2.Physics;
using PoolGame2.Player;

//this handles the HUD and any 12d information that is to be displayed on the screen
namespace PoolGame2.Game
{
    public enum OverlayMessageType
    {
        BAD_SHOT,
        START_GAME,
        REPLAY_SHOT,
    };
    public class GameScreenOverlay
    {
        
        TextureDatabase textureDatabase;
        SpriteBatch spriteBatch;
        PoolGame game;
        CameraManager cameraManager;
        Cue cue;
        SpriteFont font;

        List<GameScreenMessage> overlayMessages = new List<GameScreenMessage>();

        float currTime;         //the current gametime
        
        //once the power meter gets above a certain point, it should wobble
        //this has no effect the game, its purely visual
        int powerWobble;

        public GameScreenOverlay(TextureDatabase texdb, PoolGame game, CameraManager camManager, Cue cue)
        {
            this.textureDatabase = texdb;
            this.game = game;
            this.cameraManager = camManager;
            this.cue = cue;
            spriteBatch = new SpriteBatch(game.GraphicsDevice);
            font = game.fontDatabase.loadFont("Arial");
        }

        public void Draw(GameTime gameTime)
        {

            currTime = (float)gameTime.TotalGameTime.TotalMilliseconds;

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);

            //dont render the HUD when the balls are moving
            if (((PoolGame)game).gameManager.GetState() != ManagerGame.GameState.GAME_BALLS_ACTIVE)
            {
                //Texture2D hud = game.textureDatabase.GetTexture("hud_mock");
                //spriteBatch.Draw(hud, new Vector2(0, 0), null, Color.White);

                //DrawCueBall();
                
                DrawPowerMeter();
            }

            if (((PoolGame)game).gameManager.GetState() == ManagerGame.GameState.GAME_REPLAY_MODE)
            {
                Texture2D replay = game.textureDatabase.GetTexture("replay_camera");
                spriteBatch.Draw(replay, new Vector2(10, 10), null, Color.White);

            }
            if (((PoolGame)game).gameManager.GetState() != ManagerGame.GameState.GAME_PAUSED)
            {
                DrawScore();
                DrawOpponentScore();
                DrawCameraInfo();
                DrawOverlayMessages(gameTime);
                                
            }

            
            spriteBatch.End();

        }

        public void DrawCameraInfo()
        {
            Texture2D hud = game.textureDatabase.GetTexture("cam_hud");
            spriteBatch.Draw(hud, new Vector2(1030, 0), null, Color.White);
            int height = 18;
            Color textColor = Color.White;
            spriteBatch.DrawString(font, "Camera: " + game.gameManager.GetCameraManager().GetActiveCamera(), new Vector2(1070, height), textColor);
        }

        //overloaded method that assumes thr message is displayed for 2 seconds
        public void SetOverlayMessage(OverlayMessageType message)
        {
            SetOverlayMessage(message, 2000.0f);
        }

        public void SetOverlayMessage(OverlayMessageType message, float time)
        {
            Texture2D overlayMessage;
            Vector2 messagePos;
            if (message == OverlayMessageType.BAD_SHOT)
            {
                overlayMessage = game.textureDatabase.GetTexture("bad_shot");
                messagePos = new Vector2(300, 200);
                overlayMessages.Add(new GameScreenMessage(currTime, time,  messagePos, overlayMessage, spriteBatch) );
            }
            else if (message == OverlayMessageType.START_GAME)
            {
                overlayMessage = game.textureDatabase.GetTexture("start_game");
                messagePos = new Vector2(300, 200);
                overlayMessages.Add(new GameScreenMessage(currTime, time,  messagePos, overlayMessage, spriteBatch) );
            }
            else if (message == OverlayMessageType.REPLAY_SHOT)
            {
                overlayMessage = game.textureDatabase.GetTexture("replay_shot");
                messagePos = new Vector2(10, 20);
                overlayMessages.Add(new GameScreenMessage(currTime, time,  messagePos, overlayMessage, spriteBatch) );
            }
        }

        

        public void DrawOverlayMessages(GameTime gametime){

            foreach (GameScreenMessage message in overlayMessages){
                message.Draw(gametime);
            }

            //check to see if the messages should still be displayed
            GameScreenMessage iterMsg;
            
            for (int i =0; i< overlayMessages.Count;i++)
            {
                iterMsg = overlayMessages[i];
                if (!iterMsg.StillDisplayed(gametime))
                {
                    overlayMessages.Remove(iterMsg);
                    //System.Console.WriteLine("Removing message");
                }
                else
                {
                    //System.Console.WriteLine("Not removing message");
                }
            }

        }


        public void DrawCueBall()
        {
        }

        public void DrawScore()
        {
            Texture2D scoreHud = game.textureDatabase.GetTexture("score_hud");
            spriteBatch.Draw(scoreHud, new Vector2(0, 600), null, Color.White);
             
            int height = 630;
            Color textColor = Color.White;
            height += 45;
            PhysicsRuntime physicsRun = game.gameManager.GetPhysicsRuntime();
            PlayerPresence playerPres = game.gameManager.GetPlayerPresence(0);
            string words = "Your Score: ";
            if (playerPres.getColours()){
                if (physicsRun.getColours() < 7)
                {
                    words = "Colours: " + physicsRun.getColours() + "/7";
                }
                else
                {
                    words = "Colours: Black";
                }
            }else if (playerPres.getStripes()){
                if (physicsRun.getStripes() < 7)
                {
                    words = "Stripes: " + physicsRun.getStripes() + "/7";
                }
                else
                {
                    words = "Stripes: Black";
                }
            }else{
                words = "Balls Undecided";
            }
            if (game.gameManager.GetPlayerNumber() == 0)
            {
                words = words + " - Playing";
            }
            spriteBatch.DrawString(font, words, new Vector2(24, height), textColor);
        }


        //draws the opponents score
        public void DrawOpponentScore()
        {
            Texture2D scoreHud = game.textureDatabase.GetTexture("score_opponent_hud");
            spriteBatch.Draw(scoreHud, new Vector2(980, 600), null, Color.White);

            int height = 630;
            Color textColor = Color.White;
            height += 45;

            PhysicsRuntime physicsRun = game.gameManager.GetPhysicsRuntime();
            PlayerPresence playerPres = game.gameManager.GetPlayerPresence(1);
            string words = "Your Score: ";
            string opponent = "Opponent: ";
            if (playerPres.getColours())
            {
                if (physicsRun.getColours() < 7)
                {
                    words = "Colours: " + physicsRun.getColours() + "/7";
                }
                else
                {
                    words = "Colours: Black";
                }
            }
            else if (playerPres.getStripes())
            {
                if (physicsRun.getStripes() < 7)
                {
                    words = "Stripes: " + physicsRun.getStripes() + "/7";
                }
                else
                {
                    words = "Stripes: Black";
                }
            }
            else
            {
                words = "Balls Undecided";
            }
            if (game.gameManager.GetPlayerNumber() == 1)
            {
                words = words + " - Playing";
            }
            spriteBatch.DrawString(font, opponent, new Vector2(1050, 636), textColor);
            spriteBatch.DrawString(font, words, new Vector2(1025, height), textColor);

        }

        public void DrawPowerMeter()
        {
            Texture2D scoreHud = game.textureDatabase.GetTexture("power_hud");
            spriteBatch.Draw(scoreHud, new Vector2(-2, 203), null, Color.White);

            Texture2D meter = game.textureDatabase.GetTexture("power_meter");
            int maxHeight = 364;
            float ratio = cue.GetShotPower() / GameConstants.MAX_SHOT_POWER;
            float height = maxHeight * ratio;

            spriteBatch.Draw(meter, new Vector2(28, 590 - (int)height), new Rectangle(0, maxHeight - (int)height, 25, (int)height), Color.White);
        }
    }
}
